/*
 * @lc app=leetcode.cn id=35 lang=javascript
 *
 * [35] 搜索插入位置
 */

// @lc code=start
/**
 * @param {number[]} nums
 * @param {number} target
 * @return {number}
 */
var searchInsert = function(nums, target) {
  let left = 0
  let right = nums.length
  // if (target > nums[right]) {
  //   return right + 1
  // } else if (target < nums[left]) {
  //   return 0
  // }
  while (left < right) {
    const mid = left + (right - left) / 2 | 0
    if (target < nums[mid]) {
      right = mid
    } else if (target > nums[mid]) {
      left = mid + 1
    } else {
      return mid
    }
  }
  return left
}
// console.log(searchInsert([1,3,5,6], 2))
// @lc code=end

